import QtQuick 2.0
import QtQuick.Controls 2.14

import "../basic"

TextField {
    id: textField
    color: "#ddd"
    readOnly: true
    font.pixelSize: 20
    cursorVisible: true
    font.family: BasicConfig.commFont
    Connections {
        target: BasicConfig
        function onBlankAreaClicked() { focus = false }
    }

    Keys.onPressed: {
        if (!textField.focus) return
        let str = ""
        let flag = 0;

        if (event.modifiers & Qt.ControlModifier) {
            str += "Ctrl + "
            flag = 1;
        }
        if (event.modifiers & Qt.ShiftModifier) {
            str += "Shift + "
            flag = 1;
        }
        if (event.modifiers & Qt.AltModifier) {
            str += "Alt + "
            flag = 1;
        }

        if (1 !== flag) {
            return
        }

        str += String.fromCharCode(event.key)
        textField.text = str
    }
    background: Rectangle {
        color: BasicConfig.fieldBgColor
        border.width: 1
        border.color: textField.focus?"#333":BasicConfig.fieldBgBordColor
        anchors.fill: parent
        radius: height / 2
        Rectangle {
            id: focusRect
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            anchors.topMargin: 8
            anchors.bottomMargin: 8
            width: 1
            visible: textField.focus
            anchors.leftMargin: textField.implicitWidth
        }
        SequentialAnimation {
            id: focusAnimation
            running: textField.focus    // 文本框有焦点时运行
            loops: Animation.Infinite   // 无限循环
            PropertyAnimation {
                target: focusRect
                property: "opacity"
                from: 1
                to: 0
                duration: 250
            }
            PropertyAnimation {
                target: focusRect
                property: "opacity"
                from: 0
                to: 1
                duration: 250
            }
        }
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onEntered: {
                cursorShape = Qt.IBeamCursor
            }
            onExited: {
                cursorShape = Qt.ArrowCursor
            }
            onPressed: {
                parent.border.color = "#333"
                mouse.accepted = false
            }
        }
    }
}
